using System.Collections;

namespace Game.Core
{
	public static class GlobalEventSystem
	{
		private static Hashtable events = new Hashtable();

		private static ArrayList bindList;

		private static ArrayList unbindList;

		private static ArrayList fireList;

		private static EventObjectHandler handler;

		public static bool Bind(string eventName, EventObjectHandler handler)
		{
			if (!events.ContainsKey(eventName))
			{
				events.Add(eventName, ArrayList.Synchronized(new ArrayList()));
			}
			bindList = events[eventName] as ArrayList;
			if (!bindList.Contains(handler))
			{
				bindList.Add(handler);
				bindList = null;
				return true;
			}
			bindList = null;
			return false;
		}

		public static bool UnBind(string eventName, EventObjectHandler handler)
		{
			if (events.ContainsKey(eventName))
			{
				unbindList = events[eventName] as ArrayList;
				int num = unbindList.IndexOf(handler);
				if (num > -1)
				{
					unbindList.RemoveAt(num);
					if (unbindList.Count == 0)
					{
						events.Remove(eventName);
					}
					unbindList = null;
					return true;
				}
			}
			unbindList = null;
			return false;
		}

		public static bool Fire(BaseEvent evt)
		{
			string eventName = evt.EventName;
			if (!events.ContainsKey(eventName))
			{
				return false;
			}
			fireList = (events[eventName] as ArrayList).Clone() as ArrayList;
			int count = fireList.Count;
			for (int i = 0; i < count; i++)
			{
				handler = fireList[i] as EventObjectHandler;
				handler(evt);
			}
			handler = null;
			fireList = null;
			return true;
		}
	}
}
